﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Diagnostics;
using System.IO;
using System.Configuration;
namespace Exact_Extension
{
    public class XMLExportOld
    {
        ExactDataContext DS;
        public XMLExportOld(ExactDataContext _DS)
        {
            DS = _DS;

        }
        public void StartProcessImportInvoice(string filexml)
        {
            Process Proc = new Process();
            Proc.StartInfo.FileName = @"ASImport.exe";
            Proc.StartInfo.Arguments = "-r" + DS.Connection.DataSource +  " -D" +DS.Connection.Database + " -u -~ I -URL" + filexml + " -Tinvoices -OPT18 -Oauto";
            Proc.StartInfo.UseShellExecute = false;
            Proc.StartInfo.RedirectStandardError = true;
            Proc.StartInfo.RedirectStandardError = true;
            Proc.StartInfo.Verb = "runas";
            Proc.Start();
            Proc.WaitForExit();
            Proc.Close();
        }
        public string ExportSalesInvoice(List<ISHCMC_V_StudentListWithDebtor> ListStudent,string schoolyear)
        {
            string filename = ConfigurationSettings.AppSettings["ShareFolderXML"] +  @"\INV" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".xml";
              XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";

                XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", null));
                XElement elementRoot = new XElement("eExact", new XAttribute(XNamespace.Xmlns + "xsi", ns),
                    new XAttribute(ns + "noNamespaceSchemaLocation", "eExact-Schema.xsd"));


                doc.Add(elementRoot);
                XElement elementInvoices = new XElement("Invoices",new XAttribute("companycode",DS.Connection.Database ));
        
            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                var queryInvHeader = from InvHeader in DS.ISHCMC_InvoiceHeaders
                                     where InvHeader.StudentID == itemStudent.StudentID
                                     && InvHeader.Debcode == itemStudent.DebtorCode
                                     && InvHeader.School == itemStudent.School
                                     && InvHeader.SchoolYear == schoolyear
                                     select InvHeader;

              
             
                foreach (ISHCMC_InvoiceHeader itemInvHeader in queryInvHeader)
                {
                    XElement elementInvoiceType = new XElement("Invoice");
                    if (itemStudent.School == "ISHCMC")
                    {
                        elementInvoiceType = new XElement("Invoice",
                        
                        new XAttribute("code", "300"),new XAttribute("type", "V"));
                        elementInvoices.Add(elementInvoiceType);
                    }
                    else
                    {
                        elementInvoiceType = new XElement("Invoice",
                     
                      new XAttribute("code", "301"),new XAttribute("type", "V"));
                        elementInvoices.Add(elementInvoiceType);
                    }
                    var querySec = from Section in DS.ISHCMC_Sections
                                   where Section.DueDate == itemInvHeader.DueDate
                                   select Section;
                    ISHCMC_Section itemSec = querySec.First();
                    string invoiceno = itemInvHeader.InvoiceDisplayID.ToString();
                    while (invoiceno.Length < 5)
                    {
                        invoiceno = "0" + invoiceno;
                    }
                    XElement elementDescription = new XElement("Description",
                         DateTime.Now.Date.ToString("yy") + "-" + invoiceno + "-" + itemSec.SectionCode + "-" + itemStudent.School);
                    elementInvoiceType.Add(elementDescription);

                    XElement elementYourRef = new XElement("YourRef",
                    DateTime.Now.Date.ToString("yy") + "-" + invoiceno + "-" + itemSec.SectionCode + "-" + itemStudent.School);
                    elementInvoiceType.Add(elementYourRef);

                    XElement elementCurrency = new XElement("Currency", new XAttribute("code", "VND"));
                    elementInvoiceType.Add(elementCurrency);

                    XElement elementOrderBy = new XElement("OrderedBy");
                    elementInvoiceType.Add(elementOrderBy);
                    XElement elementOrderByCode = new XElement("Debtor",
             
                        new XAttribute("code", itemStudent.DebtorCode.Trim())
                       
                      );
                    elementOrderBy.Add(elementOrderByCode);

                    XElement elementDeliverTo = new XElement("DeliverTo");
                    elementInvoiceType.Add(elementDeliverTo);
                    XElement elementDeliverToCode = new XElement("Debtor",
                       
                        new XAttribute("code", itemStudent.DebtorCode.Trim())                       
                   );
                    elementDeliverTo.Add(elementDeliverToCode);

                    XElement elementInvoiceTo = new XElement("InvoiceTo");
                    elementInvoiceType.Add(elementInvoiceTo);
                    XElement elementInvoiceToCode = new XElement("Debtor",
                          
                        new XAttribute("code", itemStudent.DebtorCode.Trim())
                     
                      );
                    elementInvoiceTo.Add(elementDeliverToCode);

                    XElement elementWarehouse = new XElement("Warehouse" , new XAttribute("code","1"));
                    elementInvoiceType.Add(elementWarehouse);

                     XElement elementPaymentCondition = new XElement("PaymentCondition" , new XAttribute("code","00"));
                    elementInvoiceType.Add(elementPaymentCondition);

                    XElement elementDeliveryMethod = new XElement("DeliveryMethod", new XAttribute("code", "DEF"));
                    elementInvoiceType.Add(elementDeliveryMethod);
                     var queryInvDetail = from InvDetail in DS.ISHCMC_Invoices
                                          where InvDetail.InvoiceHeaderID == itemInvHeader.ID
                                          select InvDetail;
                    foreach(ISHCMC_Invoice itemInvDetail in queryInvDetail)
                    {
                        XElement elementInvoiceLine = new XElement("InvoiceLine");
                        elementInvoiceType.Add(elementInvoiceLine);

                        XElement elementItem = new XElement("Item", new XAttribute("code",itemInvDetail.Itemcode));
                        elementInvoiceLine.Add(elementItem);

                        XElement elementQuantity = new XElement("Quantity", "1");
                        elementInvoiceLine.Add(elementQuantity);

                        XElement elementUnit = new XElement("Unit", new XAttribute("unit", "-"));
                        elementInvoiceLine.Add(elementUnit);

                        XElement elementPrice = new XElement("Price");
                        elementInvoiceLine.Add(elementPrice);
                        XElement elementPriceValue = new XElement("Value", itemInvDetail.Amount);
                        elementPrice.Add(elementPriceValue);
                        if (itemInvDetail.Discount.HasValue)
                        {
                            if (itemInvDetail.Discount.Value > 0)
                            {
                                XElement elementDiscount = new XElement("Discount");
                                elementInvoiceLine.Add(elementDiscount);
                                XElement elementDiscountValue = new XElement("Percentage",  itemInvDetail.Discount.Value);
                                elementDiscount.Add(elementDiscountValue);
                            }
                        }

                        XElement elementDelivery = new XElement("Delivery");
                        elementInvoiceLine.Add(elementDelivery);
                        XElement elementDeliveryDate = new XElement("Date", "01/08/"+DateTime.Now.Year.ToString());
                        elementDelivery.Add(elementDeliveryDate);

                        XElement elementCostCenter = new XElement("CostCenter", new XAttribute("code",itemStudent.School));
                        elementInvoiceLine.Add(elementCostCenter);
                       
                  
                    }
             
                }
               
            }

            elementRoot.Add(elementInvoices);
            doc.Save(filename);
            return filename;
        }
    }
}
